<?php
/**
 * 距离现在时间
 */
function showBeforeTime($timestamp)
{
    $time_offset = time() - $timestamp;
    $minutetime = 60;
    $hourtime =  3600;
    $daytime = 86400;
    $monthtime = 2592000;
    $yeartime = 31104000;
    if ($time_offset < $minutetime){
        // 1分钟内
        return $minutetime-$time_offset.'秒前';
    } else if ($time_offset < $hourtime) {
        // 1小时内
        return ceil($time_offset / $minutetime) . '分钟前';
    } else if ($time_offset < $daytime) {
        // 24小时内
        $hour = floor($time_offset / $hourtime);
        return $hour . '小时前';
    }else if ($time_offset < $monthtime) {
        // 30天内
        $day = floor($time_offset / $daytime);
        return $day . '天前';
    } elseif ($time_offset < $yeartime) {
        //超过30天
        $month = floor($time_offset / $monthtime);
        return $month . '个月前';
    }else {
        $year = floor($time_offset/$yeartime);
        return $year . '年前';
    }
}